package org.jcb.view;

import org.jcb.ChinoisActivity;
import org.jcb.R;
import org.jcb.dao.data.ChineseDatabase;
import org.jcb.model.ChineseQuestionnaire;
import org.jcb.model.DataChineseLanguage;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LearningView extends LinearLayout {
	ChineseQuestionnaire questionnaire ;
	TextView tvChineseChar ;
	TextView tvPinyin ;
	TextView tvTranslated ;
	ImageView imageCharacter ;
	 private Button buttonNext, buttonPrev, buttonFirst, buttonRepeteSon ;  
	
	public ChineseQuestionnaire getQuestionnaire() {
		return questionnaire;
	}

	public void setQuestionnaire(ChineseQuestionnaire questionnaire) {
		this.questionnaire = questionnaire;
	}

	public LearningView(Context context,ChineseQuestionnaire questionnaire)
	 {
	   super(context);
	   this.questionnaire = questionnaire;
	   LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	   View view=layoutInflater.inflate(R.layout.learn,this);
	   tvChineseChar = (TextView) view.findViewById(R.id.chinese);
	   tvPinyin = (TextView) view.findViewById(R.id.pinyin);
	   tvTranslated = (TextView) view.findViewById(R.id.translated);
	   buttonPrev  = (Button) view.findViewById(R.id.prevChar);
	   buttonNext  = (Button) view.findViewById(R.id.nextChar);
	   buttonFirst  = (Button) view.findViewById(R.id.firstChar);
       buttonRepeteSon = (Button) view.findViewById(R.id.buttonRepeteSon);

	   //buttonLast  = (Button) view.findViewById(R.id.LastChar);
	   imageCharacter = (ImageView) view.findViewById(R.id.imageCharacter);
	  
       if (buttonRepeteSon != null) {
	        buttonRepeteSon.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	ChinoisActivity.mediaPlayer.start();
	            }
	        });
       }
	   
	   
	   buttonNext.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               Log.d("TAG", "mAddAccountButton clicked");
               nextItem();
           }
       });
	   
	   buttonPrev.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               Log.d("TAG", "mAddAccountButton clicked");
               prevItem();
           }
       });	   
	   
	   buttonFirst.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               Log.d("TAG", "mAddAccountButton clicked");
               firstItem();
           }
       });	  
	   
//	   buttonLast.setOnClickListener(new View.OnClickListener() {
//           public void onClick(View v) {
//               Log.d("TAG", "mAddAccountButton clicked");
//               lastItem();
//           }
//       });	  	   
	   
	   nextItem();
	 }


	public void firstItem() {
		try {
			questionnaire.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.i("", e.toString());
		}
		display() ;
	}
	
	public void lastItem() {
		try {
			questionnaire.end();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.i("", e.toString());
		}
		display() ;
	}
	
	public void prevItem() {
		try {
			questionnaire.prevQuestion();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.i("", e.toString());
		}
		display() ;
	}
	
	
	public void nextItem() {
		try {
			questionnaire.nextQuestion();
			display() ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.i("", e.toString());
		}
		
	}
	
	public void display() {
		DataChineseLanguage dcl = questionnaire.getDataChinese() ;
		tvChineseChar.setText(questionnaire.getChineseQuestion());
		tvPinyin.setText(questionnaire.getPinyin() );
		tvTranslated.setText(questionnaire.getTranslation());
		if (dcl.getAdrImgage().length() > 0) {
			imageCharacter.setImageResource(Integer.parseInt(dcl.getAdrImgage()));
		}
		else {
			imageCharacter.setImageResource(R.drawable.interrogation);
		}
//		if (dcl.getAdrSon().length() > 1) {
//			ChinoisActivity.mediaPlayer = MediaPlayer.create(this.getContext(), Integer.parseInt(dcl.getAdrSon()));
//			ChinoisActivity.mediaPlayer.start();
//		}
		
		int sound = ChineseDatabase.getSound(questionnaire.getPinyin());
		if (sound > 0) {
			ChinoisActivity.mediaPlayer = MediaPlayer.create(this.getContext(), sound);
			ChinoisActivity.mediaPlayer.start();
		}
		
		
	}
	
	
}
